const responseBody = {
  message: 'Request OK!',
  success: true,
  timestamp: '',
  data: null,
  code: 200
}

export const builder = (data) => {
  responseBody.data = data
  responseBody.timestamp = new Date().getTime()
  return responseBody
}

/**
 * 转换查询参数
 * @param {*} options 请求的参数
 */
export const getQueryParameters = (options) => {
  const url = options.url
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search)
    .replace(/"/g, '\\"')
    .replace(/&/g, '","')
    .replace(/=/g, '":"') + '"}')
}

/**
 * 转换options的body为对象
 * @param {*} options 请求的参数
 */
export const getBody = (options) => {
  return options.body && JSON.parse(options.body)
}
